package com.zl.project.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.zl.project.pojo.User;
import com.zl.project.pojo.supplycom;
import com.zl.project.service.supplycomService;
/**
 * 升级会员控制器
 * @author 12642
 *
 */
@Controller
@RequestMapping("supplycom")
public class supplycomController {
	
	@Autowired
	private supplycomService ss;
	
	/**
	 * 升级会员,添加用户信息到数据库
	 * @param su
	 * @param province2
	 * @param city2
	 * @param district2
	 * @param pic
	 * @param request
	 * @return
	 */
	@RequestMapping(value="inSupplycom")
	public String insertSupplycom(supplycom su,String province2
			,String city2,String district2,@RequestParam MultipartFile pic,
			HttpServletRequest request) {
		//生成随机数
		User  user  = (User) request.getSession().getAttribute("LoginUser");
		
		String uuid=UUID.randomUUID().toString();
		OutputStream os=null;
		try {
			//将图片路径合并,且将图片上传到本地
			System.out.println(user.getUsername()+"???"+user.getUserid());
			su.setUserid(user.getUserid());
			String realpath=request.getSession().getServletContext().getRealPath("/images");
			File file = new File(realpath+"/"+uuid+pic.getOriginalFilename() );
			os= new FileOutputStream(file);
		
			FileCopyUtils.copy(pic.getInputStream(), os);
			//得到省市级联与详细地址的结合体
			String conaddress =province2+city2+district2+su.getComaddress();
			//写入
			su.setComaddress(conaddress);
			
			
			su.setBusinessLicense("images/upload/"+uuid+pic.getOriginalFilename());
			ss.insertSupplycom(su);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		return "index";
	}
}
